package com.jintoufs.web.action.hotel;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.QueryRule;
import com.jintoufs.dao.hotel.HotelOrderDetailMapper;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.hotel.HotelOrder;
import com.jintoufs.domain.hotel.HotelOrderDetail;
import com.jintoufs.domain.user.User;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.hotel.HotelOrderService;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("hotel/order")
public class HotelOrderAction extends SupportAction{
    @Resource
    private HotelOrderService hotelOrderService;
    @Resource
    private HotelOrderDetailMapper hotelOrderDetailMapper;

    @RequestMapping("list")
    public String list(Model model) {
        return "hotels/order/list";
    }

    @RequestMapping("query")
    @ResponseBody
    @SystemControllerLog(description = "获取酒店订单列表")
    public JqgridPage query(int page,
                        int rows, String sidx,
                        String sord,
                        String filters) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);

        List<Map<String,Object>> hotels = hotelOrderService.queryByFilters(filters_);
        PageInfo pageInfo = new PageInfo(hotels);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }

    @RequestMapping(value="/detail")
    public String detail(String id, Model model) {
        HotelOrderDetail hotelOrderDetail = hotelOrderDetailMapper.selectByHotelOrderId(id).get(0);
        HotelOrder hotelOrder = hotelOrderService.getById(id);
        model.addAttribute("orderDetail", hotelOrderDetail);
        model.addAttribute("hotelOrder", hotelOrder);
        return "hotels/order/detail";
    }

}
